package com.rgs.base.core.exception;

import com.rgs.base.core.model.WebApiReturnResultModel;
import com.rgs.base.core.system.WebResponseState;
import com.rgs.base.core.util.RgsJson;

/**
 * @author Ren Gaoshuai
 * @ClassName BaseException
 * @date 2021-12-24 17:14
 **/
public class BaseException extends RuntimeException {
    /**
     * 所属模块
     */
    private String module;
    /**
     * 错误消息
     */
    private String defaultMessage;

    private Integer errCode;

    private WebResponseState webResponseState;

    public BaseException(String module, String defaultMessage) {
        this.module = module;
        this.defaultMessage = defaultMessage;
        this.errCode = 500;
    }

    public BaseException(String module, WebResponseState webResponseState) {
        this.module = module;
        this.webResponseState = webResponseState;
        this.defaultMessage = webResponseState.getStandardMessage();
        this.errCode = webResponseState.getCode();
    }

    public WebApiReturnResultModel<Void> getWebApiReturnResultModel() {
        return WebApiReturnResultModel.ofStatus(webResponseState, defaultMessage);
    }

    @Override
    public String getMessage() {
        return "所属模块:".concat(module).concat("。错误消息:").concat(defaultMessage);
    }

    @Override
    public String toString() {
        return "WuXiHuaJieCommonException@" + RgsJson.toJSONString(WebApiReturnResultModel.ofMessage(errCode, defaultMessage));
    }
}

